package org.yiming.tools.mqtt.client.client.command;

import lombok.RequiredArgsConstructor;
import org.yiming.tools.mqtt.client.ApplicationContext;
import org.yiming.tools.mqtt.client.client.MqttClient;
import org.yiming.tools.mqtt.client.command.Command;

/**
 * @author renwenpeng
 * date  2022/3/26 5:19 下午
 */
@RequiredArgsConstructor
public class ConnCommand implements Command {

    private final ApplicationContext applicationContext;

    private final String[] names = new String[]{"conn"};

    @Override
    public String[] name() {
        return names;
    }

    @Override
    public String execute(String[] args) {
        // 校验args
        int argsMinLength = 4;
        if (args == null || args.length < argsMinLength) {
            return "args incorrect";
        }
        MqttClient mqttClient = new MqttClient(applicationContext.getThreadPoolTaskExecutor()
                , applicationContext.getMqttConfigFactory().newInstance(args[0], args[1], args[2]), args[3]);
        applicationContext.putMqttClient(mqttClient);
        mqttClient.init(null);
        return null;
    }

    @Override
    public String[] helpContent() {
        return new String[]{"conn", "{url:tcp://127.0.0.1:9530} {name:testuser} {password:123456} {clientId}"};
    }
}
